<template>
  <div>
    <Loading :loading="loading">
      <Form :ref="formRef" :rules="formDataCheckRules" :label-width="90" :model="formData" inline>
        <FormItem prop="classifyCode" label="分类编码:" style="width:90%">
          <Input type="text" v-model="formData.classifyCode" placeholder="请输入" />
        </FormItem>
        <FormItem prop="classifyName" label="分类名称:" style="width:90%">
          <Input type="text" v-model="formData.classifyName" placeholder="请输入" />
        </FormItem>
        <FormItem prop="taxRate" label="分类税率:" style="width:50%">
          <Input type="text" v-model="formData.taxRate" placeholder="请输入">
            <span slot="prepend"></span>
            <span slot="append">%</span>
          </Input>
        </FormItem>
        <FormItem prop="businessCode" label="商品编码:" style="width:90%">
          <Input type="text" v-model="formData.businessCode" placeholder="请输入" />
        </FormItem>
        <FormItem prop="businessName" label="商品名称:" style="width:90%">
          <Input type="text" v-model="formData.businessName" placeholder="请输入" />
        </FormItem>
        <FormItem prop="price" label="商品单价:" style="width:90%">
          <Input type="text" v-model="formData.price" placeholder="请输入" />
        </FormItem>
        <FormItem :style="{width: '90%'}">
          <div :style="{textAlign: 'right'}">
            <Button type="primary" @click="handleSubmit">提交</Button>
          </div>
        </FormItem>
      </Form>
    </Loading>
  </div>
</template>
<script>
import config from "@/utils/config";
import { namespace } from "../module";
import {InputNumber,Input,Button,Form,FormItem,Message} from "view-design";
import Loading from "@/components/Loading";
import Uuid from "uuid";
export default {
  name: "ItemsAddForm",
  components: {InputNumber,Loading,Input,Button,Form,FormItem,Message},
  props : {
    taxpayerId: String
  },
  data: function() {
    let vm = this;
    return {
      formRef: Uuid.v1(),
      formData: {}
    };
  },
  methods: {
    handleSubmit: function() {
      let vm = this;
      this.$refs[this.formRef].validate(async valid => {
        if (valid) {
          vm.formData.taxpayerId = vm.taxpayerId;
          let response = await vm.$store.dispatch(`${namespace}/save`,vm.formData);
          if (response.errorCode === 0) {
            vm.$emit("closeAdd");
            this.$Message.success("提交成功");
            vm.$emit("reloadItems");
          }
        }
      });
    }
  },
  computed: {
    formDataCheckRules: function() {
      return {
        classifyCode: [
          {required: true,message: "请输入",trigger: "blur",type: "string"}
        ],
        classifyName: [
          {required: true,message: "请输入",trigger: "blur",type: "string"}
        ],
        taxRate: [
          {required: true,message: "请输入",trigger: "blur"},
          {pattern: /^[1-9]\d*$/, message: "请输入正整数", trigger: "blur"}
        ],
        businessCode: [
          {required: true,message: "请输入",trigger: "blur",type: "string"}
        ],
        businessName: [
          {required: true,message: "请输入",trigger: "blur",type: "string"}
        ],
        price: [
          {required: true,message: "请输入",trigger: "blur"},
          {pattern: /^[0-9]+([.]{1}[0-9]+){0,1}$/, message: "请输入数字", trigger: "blur"}
        ]
      };
    },
    loading: function() {
      let result = this.$store.state[namespace].loading;
      return result;
    }
  }
};
</script>

<style>
</style>
